package logic

import (
	"context"
	"fmt"
	"serve/internal/global/model"
	"serve/internal/svc"
	"serve/videoSrv"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoShowLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoShowLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoShowLogic {
	return &VideoShowLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VideoShowLogic) VideoShow(in *videoSrv.VideoShowRequest) (*videoSrv.VideoShowResponse, error) {
	// todo: add your logic here and delete this line
	var user model.User
	model.DB.Where("id = ?", in.Uid).Find(&user)
	var vip = 1
	if user.VipEnd.IsZero() {
		vip = 1
	} else {
		vip = 2
	}
	var video model.Video
	model.DB.Where("id = ?", in.Vid).Find(&video)
	var vipvideo = model.VideoVipGet{
		Vid:         int(in.Vid),
		Uid:         int(in.Uid),
		Vip:         vip,
		LoginStatus: 0,
		VipEnd:      user.VipEnd,
	}
	status, ok := vipvideo.UseVipShow()
	if ok {
		return &videoSrv.VideoShowResponse{
			VideoUrl: video.Url,
			ImageUrl: video.ImageUrl,
			Title:    video.Title,
			Len:      int64(video.Len),
		}, nil
	} else {
		return nil, fmt.Errorf(status)
	}

}
